var game = new Game();
Game.prototype.init = function () {
    this.snake = new Snake();
    this.snake.direction = "right";
    this.snake.init();
    window.food = SquareFactory.create("Food");
    this.food = food;
    this.food.renderFood(this.snake);
    this.timer = null;
    this.bindEvent();
}

Game.prototype.bindEvent = function () {
    document.addEventListener("keydown", e => {
        // ArrowLeft
        // ArrowRight
        // ArrowDown
        // ArrowUp
        if(this.timer === null) {
            return ; // 开始游戏未点击时，不能修改direction
        } 
        if (e.key === 'ArrowLeft' && this.snake.direction !== "right") {
            this.snake.direction = "left";
        } else if (e.key === 'ArrowRight' && this.snake.direction !== "left") {
            this.snake.direction = "right";
        } else if (e.key === 'ArrowDown' && this.snake.direction !== "top") {
            this.snake.direction = "bottom";
        } else if (e.key === 'ArrowUp' && this.snake.direction !== "bottom") {
            this.snake.direction = "top"
        }
    })
    document.getElementById("btn").onclick = () => {
        this.startGame();
    }
}

Game.prototype.startGame = function () {
    clearInterval(this.timer);
    this.timer = setInterval(() => {
        this.snake.move();
    }, time)
}
Game.prototype.gameOver = function () {
    this.startGame = ()=>{};
    clearInterval(this.timer);
    alert(s.realDom.innerText);
}

eventBus.on("gameOver",()=> {
    Game.prototype.gameOver.call(game); // 其实还是依赖全局变量 设计的不是很好
})

game.init();
